package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public void save(Role role) {
        role.setId(UUID.randomUUID().toString());
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void delete(String id) {
        roleDao.delete(id);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public PageInfo findAll(int page, int size, String companyId) {
        PageHelper.startPage(page,size);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo(list);
    }

    //查询所有
    public List findAll(String loginCompanyId) {
        return roleDao.findAll(loginCompanyId);
    }

    @Override
    public List<String> findModuleIdsByRoleId(String roleId) {
        return roleDao.findModuleIdsByRoleId(roleId);
    }

    //对角色分配权限
    public void updateRoleModule(String roleId, String moduleIds) {
        //1、根据角色id删除角色模块中间表数据
        roleDao.deleteRoleModule(roleId);
        //2、将模块id字符串，已“，”切割为模块id数组
        String[] ids = moduleIds.split(",");
        //3、循环模块id数组
        for (String moduleId : ids) {
            //4、构造数据，向角色模块中间表添加数据
            roleDao.saveRoleModule(roleId,moduleId);
        }
    }
}
